/*****************************************************************************************
   
   Copyright (c) Microsoft Corporation. All rights reserved.

   Use of this code sample is subject to the terms of the Microsoft
   Permissive License, a copy of which should always be distributed with
   this file.  You can also access a copy of this license agreement at:
   http://www.microsoft.com/resources/sharedsource/licensingbasics/permissivelicense.mspx

 ****************************************************************************************/

using System;

namespace GrooveCommands
{
	class GrooveSendMessage : CommandUtils.GrooveUtilBase
	{
		private string[] m_ContactURIs;
		private string m_Message;

		[STAThread]
		static void Main(string[] i_Arguments)
		{
			CommandUtils.UtilAppMain.RunApplication(new GrooveSendMessage(i_Arguments));
		}

		public GrooveSendMessage(string[] i_Arguments)
			: base (i_Arguments)
		{
			base.UsageMessage = "GrooveSendMessage [PARAMS...]";
			base.HelpMessage = 
				"GrooveSendMessage\r\n\t" + 
				"Sends message to specified Groove contact(s)\r\n" +
				"Parameters:\r\n\t" +
				"/Contacts=           Comma delimited Web Services URIs for Groove contacts\r\n\t" +
				"/Message=            The simple text body of the message\r\n\t" +
				"[/Account=           The Web Services URI for the Groove account]\r\n\t" +
				"[/Identity=          The Web Services URI for the Groove identity]\r\n\t" +
				"[/RequestKey=        Token used to communicate Groove host]\r\n\t" +
				"[/Host=              The remote Groove host name to issue commands to]\r\n\t" +
				"[/Port=              The remote Groove host port to issue commands to]\r\n\t" +
				"[/Timeout=           Timeout (in ms) for Web Services method execution]\r\n\t" +
				"[/X=                 Configuration file to use for parameter input]\r\n\t" +
				"\r\n";
		}

		public override CheckResult CheckParameters()
		{
			CommandUtils.UtilBase.CheckResult CheckResult = base.CheckParameters();
			if (CheckResult != CommandUtils.UtilBase.CheckResult.Succeeded )
				return CheckResult;
		
			string ContactURI = base.GetParameterValue("Contacts");
			if (System.String.IsNullOrEmpty(ContactURI))
			{
				base.DisplayUsage("You must specify contact(s).");
				return CommandUtils.UtilBase.CheckResult.Failed;
			}

			m_ContactURIs = ContactURI.Split(new char[]{','});

			m_Message = base.GetParameterValue("Message");
			if (System.String.IsNullOrEmpty(m_Message))
			{
				base.DisplayUsage("You must specify a message.");
				return CommandUtils.UtilBase.CheckResult.Failed;
			}

			return base.CheckUnusedParameters();
		}

		public override CommandUtils.UtilBase.CheckResult Run()
		{
			try
			{
				base.Identity.SendMessage
					(
					m_ContactURIs,
					m_Message
					);
			}
			catch (System.Exception Exception)
			{
				base.StandardError.WriteLine("Unable to send message: " + Exception.Message);
				throw Exception;
			}

			return CommandUtils.UtilBase.CheckResult.Succeeded;
		}
	}

}
